﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.HVAC;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using HYCreateDuct.FormDuct;
using YRevitJig;

namespace HYCreateDuct.MethodCreateDuct
{
	public class DuctCreateJigPolarAxis : PELineJig
	{
		public DuctCreateJigPolarAxis(UIDocument uiDoc, FormCreateDuct cdt) : base(uiDoc, cdt)
		{
			DuctWindowStruct data = cdt.GetData(true);
			this.dWidth = data.dDuctWidth / 304.8;
			this.dHeight = data.dDuctHeight / 304.8;
			this.nType = data.nStyle;
			this.bmmozu = data.dBimozu;
			this.cdtAll = cdt;
			base.JigStage = PELineJig.EStage.eNoOper;
			base.ResetPoint = XUiJigBase.EResetPoint.eByAxis;
			this.doc = uiDoc.Document;
			this.m_UserForm = cdt;
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Remove(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
			this.OnMouseMoveIntoFormClientEvent = (WndMsgManager.MouseMoveIntoFormClientEvnetHandler)Delegate.Remove(this.OnMouseMoveIntoFormClientEvent, new WndMsgManager.MouseMoveIntoFormClientEvnetHandler(this.OnMouseMoveIntoFormClientEvnetHandler));
		}

		private void OnMouseLeaveFormClientEvnetHandler(object sender, EventArgs e)
		{
			System.Windows.Forms.Form userForm = this.m_UserForm;
		}

		private void OnMouseMoveIntoFormClientEvnetHandler(object sender, EventArgs e)
		{
			System.Windows.Forms.Form userForm = this.m_UserForm;
		}

		public XYZ PickStartPoint(XYZ startPoint, string strPrompt = "请确定直线起点：")
		{
			try
			{
				base.JigStage = PELineJig.EStage.eStartPt;
				base.StartPoint = startPoint;
			}
			catch
			{
				base.StartPoint = null;
			}
			base.JigStage = PELineJig.EStage.eNoOper;
			return base.StartPoint;
		}

		public new XYZ PickEndPoint(XYZ ptStart, string strPrompt = "请确定直线终点：")
		{
			try
			{
				base.JigStage = PELineJig.EStage.eEndPt;
				base.StartPoint = ptStart;
				base.EndPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				base.EndPoint = this.GetResetPoint(base.StartPoint, base.EndPoint);
			}
			catch
			{
				if (this.m_prompDlg != null && base.CmdEndType == XUiJigBase.ECmdEndType.eEnter)
				{
					base.CmdEndType = XUiJigBase.ECmdEndType.eOther;
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					xyz = this.GetResetPoint(base.StartPoint, xyz);
					double num = Convert.ToDouble(this.m_prompDlg.GetInputValue()) / 304.8;
					XYZ xyz2 = (xyz - base.StartPoint).Normalize();
					base.EndPoint = base.StartPoint + xyz2 * num;
				}
				else
				{
					base.EndPoint = null;
				}
			}
			base.JigStage = PELineJig.EStage.eNoOper;
			return base.EndPoint;
		}

		public new XYZ GetResetPoint(XYZ ptBase, XYZ ptUser)
		{
			XYZ result = ptUser;
			if (base.ResetPoint == XUiJigBase.EResetPoint.eByAngle)
			{
				result = JigFuncs.GetProjectPoint(base.UiDoc, ptUser, ptBase, base.LineAngle);
			}
			else if (base.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
			{
				result = JigFuncs.GetOrthoPoint(base.UiDoc, ptUser, ptBase);
			}
			else if (base.ResetPoint == XUiJigBase.EResetPoint.eByAxis)
			{
				XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, ptUser, ptBase);
				if (axisPoint != null)
				{
					result = axisPoint;
				}
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (!this.m_ptCur.IsEmpty && paintEventArgs != null && base.JigStage == PELineJig.EStage.eEndPt)
			{
				DuctWindowStruct data = this.cdtAll.GetData(true);
				this.dWidth = data.dDuctWidth / 304.8;
				this.dHeight = data.dDuctHeight / 304.8;
				XYZ xyz = base.Client2Revit(this.m_ptCur);
				XYZ xyz2 = xyz;
				double num = base.StartPoint.DistanceTo(xyz2);
				num = num * 304.8 / 1000.0 * this.bmmozu;
				this.cdtAll.lbPyPa.Text = "Py(Pa)=" + Math.Round(num, 2);
				if (base.ResetPoint == XUiJigBase.EResetPoint.eByAngle)
				{
					XYZ projectPoint = JigFuncs.GetProjectPoint(base.UiDoc, xyz, base.StartPoint, base.LineAngle);
					this.m_ptCur = base.Revit2Client(projectPoint);
				}
				else if (base.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
				{
					XYZ orthoPoint = JigFuncs.GetOrthoPoint(base.UiDoc, xyz, base.StartPoint);
					this.m_ptCur = base.Revit2Client(orthoPoint);
				}
				else if (base.ResetPoint == XUiJigBase.EResetPoint.eByAxis)
				{
					XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, xyz, base.StartPoint);
					if (axisPoint != null)
					{
						xyz2 = axisPoint;
					}
				}
				if (!base.StartPoint.IsAlmostEqualTo(new XYZ(xyz2.X, xyz2.Y, base.StartPoint.Z)))
				{
					if ((int)this.doc.ActiveView.ViewType == 3 || (int)this.doc.ActiveView.ViewType == 117)
					{
						List<XYZ> list = new List<XYZ>();
						if (this.nType == 7 || this.nType == 8 || this.nType == 9)
						{
							list.Add(new XYZ(base.StartPoint.X, base.StartPoint.Y, base.StartPoint.Z + this.dHeight));
							list.Add(new XYZ(xyz2.X, xyz2.Y, xyz2.Z + this.dHeight));
							list.Add(new XYZ(base.StartPoint.X, base.StartPoint.Y, base.StartPoint.Z));
							list.Add(new XYZ(xyz2.X, xyz2.Y, xyz2.Z));
						}
						if (this.nType == 4 || this.nType == 5 || this.nType == 6)
						{
							list.Add(new XYZ(base.StartPoint.X, base.StartPoint.Y, base.StartPoint.Z + this.dHeight / 2.0));
							list.Add(new XYZ(xyz2.X, xyz2.Y, xyz2.Z + this.dHeight / 2.0));
							list.Add(new XYZ(base.StartPoint.X, base.StartPoint.Y, base.StartPoint.Z - this.dHeight / 2.0));
							list.Add(new XYZ(xyz2.X, xyz2.Y, xyz2.Z - this.dHeight / 2.0));
						}
						if (this.nType == 1 || this.nType == 2 || this.nType == 3)
						{
							list.Add(new XYZ(base.StartPoint.X, base.StartPoint.Y, base.StartPoint.Z));
							list.Add(new XYZ(xyz2.X, xyz2.Y, xyz2.Z));
							list.Add(new XYZ(base.StartPoint.X, base.StartPoint.Y, base.StartPoint.Z - this.dHeight));
							list.Add(new XYZ(xyz2.X, xyz2.Y, xyz2.Z - this.dHeight));
						}
						this.resultLines = new List<Line>
						{
							YJKLineEx.YJKGetBound(list[0], list[1])
						};
						XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						foreach (Line curve in this.resultLines)
						{
							new XLine2D(base.Revit2Client(curve.GetEndPoint(0)), base.Revit2Client(curve.GetEndPoint(1))).Draw(g);
						}
						this.resultLines = new List<Line>
						{
							YJKLineEx.YJKGetBound(list[2], list[3])
						};
						XGraphics g2 = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						foreach (Line curve2 in this.resultLines)
						{
							new XLine2D(base.Revit2Client(curve2.GetEndPoint(0)), base.Revit2Client(curve2.GetEndPoint(1))).Draw(g2);
						}
						this.resultLines = new List<Line>
						{
							YJKLineEx.YJKGetBound(list[0], list[2])
						};
						XGraphics g3 = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						foreach (Line curve3 in this.resultLines)
						{
							new XLine2D(base.Revit2Client(curve3.GetEndPoint(0)), base.Revit2Client(curve3.GetEndPoint(1))).Draw(g3);
						}
						this.resultLines = new List<Line>
						{
							YJKLineEx.YJKGetBound(list[1], list[3])
						};
						XGraphics g4 = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						using (List<Line>.Enumerator enumerator = this.resultLines.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								Line curve4 = enumerator.Current;
								new XLine2D(base.Revit2Client(curve4.GetEndPoint(0)), base.Revit2Client(curve4.GetEndPoint(1))).Draw(g4);
							}
							return;
						}
					}
					IList<XYZ> list2 = new List<XYZ>();
					list2.Add(base.StartPoint);
					list2.Add(xyz2);
					if (data.nHorizontalOffset != 0.0)
					{
						XYZ startPoint = base.StartPoint;
						XYZ xyz3 = xyz2;
						XYZ newXYZDeriction = YJKRevitTools.getNewXYZDeriction(startPoint, xyz3);
						Line pipeline = YJKLineEx.YJKGetBound(startPoint, xyz3);
						XYZ item = CreateDuct.getNewParallelPipeXYZ(startPoint, pipeline, data.nHorizontalOffset, newXYZDeriction);
						XYZ item2 = CreateDuct.getNewParallelPipeXYZ(xyz3, pipeline, data.nHorizontalOffset, newXYZDeriction);
						if (Math.Abs(startPoint.X - xyz3.X) < 0.0001 && Math.Abs(startPoint.Y - xyz3.Y) < 0.0001 && Math.Abs(startPoint.Z - xyz3.Z) > 0.015)
						{
							item = new XYZ(startPoint.X + data.nHorizontalOffset / 304.8, startPoint.Y, startPoint.Z);
							item2 = new XYZ(xyz3.X + data.nHorizontalOffset / 304.8, xyz3.Y, xyz3.Z);
						}
						list2.Clear();
						list2.Add(item);
						list2.Add(item2);
					}
					IList<XYZ> list3 = new List<XYZ>();
					IList<XYZ> list4 = new List<XYZ>();
					if (this.nType == 2 || this.nType == 5 || this.nType == 8)
					{
						DuctCreateJigPolarAxis.OffsetCurve(list2, this.dWidth / 2.0, out list3, 0.0);
						DuctCreateJigPolarAxis.OffsetCurve(list2, -this.dWidth / 2.0, out list4, 0.0);
					}
					else if (this.nType == 3 || this.nType == 6 || this.nType == 9)
					{
						DuctCreateJigPolarAxis.OffsetCurve(list2, 0.0, out list3, 0.0);
						DuctCreateJigPolarAxis.OffsetCurve(list2, -this.dWidth, out list4, 0.0);
					}
					else if (this.nType == 1 || this.nType == 4 || this.nType == 7)
					{
						DuctCreateJigPolarAxis.OffsetCurve(list2, this.dWidth, out list3, 0.0);
						DuctCreateJigPolarAxis.OffsetCurve(list2, 0.0, out list4, 0.0);
					}
					this.resultLines = new List<Line>
					{
						YJKLineEx.YJKGetBound(list3[0], list3[1])
					};
					XGraphics g5 = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					foreach (Line curve5 in this.resultLines)
					{
						new XLine2D(base.Revit2Client(curve5.GetEndPoint(0)), base.Revit2Client(curve5.GetEndPoint(1))).Draw(g5);
					}
					this.resultLines = new List<Line>
					{
						YJKLineEx.YJKGetBound(list4[0], list4[1])
					};
					XGraphics g6 = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					foreach (Line curve6 in this.resultLines)
					{
						new XLine2D(base.Revit2Client(curve6.GetEndPoint(0)), base.Revit2Client(curve6.GetEndPoint(1))).Draw(g6);
					}
					this.resultLines = new List<Line>
					{
						YJKLineEx.YJKGetBound(list3[0], list4[0])
					};
					XGraphics g7 = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					foreach (Line curve7 in this.resultLines)
					{
						new XLine2D(base.Revit2Client(curve7.GetEndPoint(0)), base.Revit2Client(curve7.GetEndPoint(1))).Draw(g7);
					}
					this.resultLines = new List<Line>
					{
						YJKLineEx.YJKGetBound(list3[1], list4[1])
					};
					XGraphics g8 = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					foreach (Line curve8 in this.resultLines)
					{
						new XLine2D(base.Revit2Client(curve8.GetEndPoint(0)), base.Revit2Client(curve8.GetEndPoint(1))).Draw(g8);
					}
				}
			}
		}

		private List<XYZ> TansPoint(XYZ s, XYZ e, int nType, double offset)
		{
			XYZ xyz = (e - s).Normalize();
			XYZ xyz2 = new XYZ(0.0, 0.0, -1.0);
			List<XYZ> list = new List<XYZ>();
			if (nType == 0)
			{
				XYZ xyz3 = xyz.CrossProduct(xyz2);
				list.Add(s + offset / 2.0 * xyz3);
				list.Add(e + offset / 2.0 * xyz3);
				list.Add(s + -offset / 2.0 * xyz3);
				list.Add(e + -offset / 2.0 * xyz3);
			}
			return list;
		}

		public static bool OffsetCurve(IList<XYZ> srcPts, double dOffSet, out IList<XYZ> offsetPoints, double dZOffset = 0.0)
		{
			offsetPoints = new List<XYZ>();
			IList<XYZ> list = new List<XYZ>();
			for (int i = 0; i < srcPts.Count; i++)
			{
				if (i == 0)
				{
					list.Add(srcPts[i]);
				}
				else if (srcPts[i].DistanceTo(list.Last<XYZ>()) > 0.001)
				{
					list.Add(srcPts[i]);
				}
			}
			if (list.Count < 2)
			{
				return false;
			}
			XYZ xyz = new XYZ(0.0, 0.0, 1.0) * dZOffset;
			if (list.Count == 2)
			{
				XYZ xyz2 = list[1] - list[0];
				xyz2 = new XYZ(-xyz2.Y, xyz2.X, 0.0);
				xyz2 = xyz2.Normalize();
				offsetPoints.Add(list[0] - dOffSet * xyz2 + xyz);
				offsetPoints.Add(list[1] - dOffSet * xyz2 + xyz);
				return true;
			}
			for (int j = 1; j < list.Count - 1; j++)
			{
				XYZ xyz3 = list[j - 1];
				XYZ xyz4 = list[j];
				XYZ xyz5 = list[j + 1];
				XYZ xyz6 = xyz4 - xyz3;
				xyz6 = new XYZ(xyz6.X, xyz6.Y, 0.0);
				xyz6 = xyz6.Normalize();
				XYZ xyz7 = new XYZ(-xyz6.Y, xyz6.X, 0.0);
				XYZ xyz8 = xyz5 - xyz4;
				xyz8 = new XYZ(xyz8.X, xyz8.Y, 0.0);
				xyz8 = xyz8.Normalize();
				XYZ xyz9 = new XYZ(-xyz8.Y, xyz8.X, 0.0);
				if (j == 1)
				{
					offsetPoints.Add(xyz3 - dOffSet * xyz7 + xyz);
				}
				if (xyz7.IsAlmostEqualTo(xyz9))
				{
					offsetPoints.Add(xyz4 - dOffSet * xyz7 + xyz);
				}
				else
				{
					XYZ xyz10 = xyz7 + xyz9;
					xyz10 = xyz10.Normalize();
					double d = xyz7.AngleTo(xyz10);
					offsetPoints.Add(xyz4 - dOffSet * xyz10 / Math.Cos(d) + xyz);
				}
				if (j == list.Count - 2)
				{
					offsetPoints.Add(xyz5 - dOffSet * xyz9 + xyz);
				}
			}
			return true;
		}

		private System.Drawing.Point Revit2Client()
		{
			throw new NotImplementedException();
		}

		public List<Line> resultLines = new List<Line>();

		private double dWidth = 200.0;

		private double dHeight = 200.0;

		private int nType = 2;

		private FormCreateDuct cdtAll;

		private double bmmozu;

		private Document doc;
	}
}
